package com.fenxiangle.yueding.feature.order.view;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.TextView;

import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.common.httprequestbody.HttpRequestBody;
import com.fenxiangle.yueding.common.widget.RatingBar;
import com.fenxiangle.yueding.entity.bo.PublishDemandBo;
import com.fenxiangle.yueding.entity.bo.RatingBo;
import com.fenxiangle.yueding.framework.api.UserApi;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;

public class ToRatingActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.iv_my_rating_head)
    ImageView mIvMyRatingHead;
    @BindView(R.id.rb_queestion_comment_star)
    RatingBar mRbQueestionCommentStar;
    @BindView(R.id.btn_publish_invite_)
    TextView mBtnPublishInvite;
    @BindView(R.id.iv_my_rating_name)
    TextView mIvMyRatingName;
    @BindView(R.id.iv_my_rating_cirdet)
    TextView mIvMyRatingCirdet;
    private float mRtingCount = 0;
    private PublishDemandBo publishDemandBo;

    @Override
    protected void initView() {
        initToolBar(mToolbar, "评分", true, true);
    }

    @Override
    protected int attachLayoutRes() {

        return R.layout.activity_to_rating;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initData() {
        publishDemandBo = (PublishDemandBo) this.getIntent().getSerializableExtra("rating");
        if (publishDemandBo != null) {
            AM.image().bindToCircleObject(publishDemandBo.getUserHead(), mIvMyRatingHead);
            mIvMyRatingName.setText(publishDemandBo.getUserName());
            mIvMyRatingCirdet.setText("信用金：￥" + publishDemandBo.getCredit());
        }
    }

    @Override
    protected void initEvent() {
        mRbQueestionCommentStar.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float ratingCount) {
                mRtingCount = ratingCount;
            }
        });
    }

    @OnClick(R.id.btn_publish_invite_)
    public void onViewClicked() {
        RatingBo ratingBo = new RatingBo();
        ratingBo.setOrderId(publishDemandBo.getOrderId());
        ratingBo.setScore(mRtingCount);
        ratingBo.setUserUid(publishDemandBo.getUserUid());
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("orderId", publishDemandBo.getOrderId());
            JSONArray jsonArray = new JSONArray();
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("userUid", publishDemandBo.getUserUid());
            jsonObject1.put("score", mRtingCount);
            jsonArray.put(jsonObject1);
            jsonObject.put("user", jsonArray);
            RequestBody requestBody = HttpRequestBody.getRequestBody(jsonObject);
            AM.api().createApiService(UserApi.class)
                    .toComment(requestBody)
                    .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                    .subscribe(new Observer<String>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            showLoading(d);
                        }

                        @Override
                        public void onNext(String s) {
                            dismissLoading();
                            showMsg("评价成功！");
                            finish();
                        }

                        @Override
                        public void onError(Throwable e) {
                            showMsg(e.getMessage());
                            dismissLoading();
                        }

                        @Override
                        public void onComplete() {
                            dismissLoading();
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
